﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EKTween : MonoBehaviour {
	
	static public List<object> repoArgs = new List<object>();
	
	public enum EaseType {
		EaseOutQuard,
		Linear
	}
	
	public enum CallBackType {
		OnStart,
		OnComplete,
		OnUpdate
	}

	//Get dictionary from array of arguments
	static public Dictionary<string,object> EKDict (params object[] args) {
		
		Dictionary<string,object> resultDict = new Dictionary<string, object>();
		
		if(args.Length % 2 != 0) {
			Debug.LogError("EKTween paramters error: Number of parameter cannot be");
			return null;
		}
		
		//Browser input parameters using to add to dictionary
		int i = 0;
		while(i < args.Length - 1) {
			resultDict.Add(args[i].ToString().ToLower(), args[i+1]);
			i = i + 2;
		}

		return resultDict;
	}
	
	//Generate id for new tweener
	static private string GenerateTweenID () {
		int strlen = 15;
		char[] chars = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8'};
		int num_chars = chars.Length - 1;
		string randomChars = "";
		for (int i = 0; i < strlen; i++) {
			randomChars = randomChars + chars[(int)Mathf.Floor(Random.Range(0,num_chars))];
		}
		
		return randomChars;
	}
	
	//Kill tweener instance
	static public void Kill (GameObject target) {
		
		object[] tweeners = (object[])target.GetComponents(typeof(Tweener));
		
		foreach(Tweener tweener in tweeners) {
			Destroy(tweener);
		}
	}
	
	//Kill tweener instance
	static public void Kill (Tweener tweener) {
		Destroy(tweener);
	}
	
	static public void MoveTo (GameObject target, Dictionary<string,object> args) {
		CreateTweenIstance(target, "move", "to", args);
	}

	static public void RotateTo (GameObject target, Dictionary<string,object> args) {
		CreateTweenIstance(target, "rotate", "to", args);
	}
	
	static public void ValueTo (GameObject target, Dictionary<string,object> args) {
		CreateTweenIstance(target, "value", "", args);
	}
	
	//Create new instance of tweener for target object
	static private void CreateTweenIstance (GameObject target, string tweenType, string tweenMethod, Dictionary<string,object> args) {
		
		//Prepare primary parameters for tweener
		string newTweenID = GenerateTweenID();
		args.Add("id", newTweenID); //Tween id
		args.Add("type", tweenType); //Tween type
		args.Add("method", tweenMethod); //Tween method
		args.Add("target", target); //Target object that be effected by this tweener
		
		//Add args of new instance into repositive list
		repoArgs.Add(args);
		
		//Init tweener
		target.AddComponent<Tweener>();
	}
	
}